package com.czy.learning.questionnature.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czy.learning.questionnature.dto.QuestionNatureDTO;
import com.czy.learning.questionnature.mapper.QuestionNatureMapper;
import com.czy.learning.questionnature.service.QuestionNatureService;
import com.lin.learning.questionmanagement.mapper.QuestionMapper;
import com.lin.learning.questionmanagement.service.QuestionService;
import com.lin.learning.questionmanagement.utils.BeanCopyUtils;
import com.lin.learning.questionmanagement.vo.PageVo;
import com.our.learning.infrastructure.constant.AppHttpCodeEnum;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.Question;
import com.our.learning.infrastructure.po.QuestionNature;
import com.our.learning.infrastructure.pojo.Response;
import com.our.learning.infrastructure.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author :czy
 * @date : 2023/6/22 20:17
 */
@Service
public class QuestionNatureServiceImpl extends ServiceImpl<QuestionNatureMapper, QuestionNature> implements QuestionNatureService {

    @Autowired
    private  QuestionNatureMapper questionNatureMapper;
    @Autowired
    private QuestionService questionService;


    @Override
    public ResponseResult dividePage(Integer page, Integer perPage,Integer id,String knowledge,String chapter) {
        QueryWrapper<QuestionNature> queryWrapper = new QueryWrapper<>();
        Page<QuestionNature> pages=new Page<>(page,perPage);

        Map<String,Object> map = new HashMap<>();
        if(id != null && knowledge != null && chapter != null){
            map.put("id",id);
            map.put("knowledge",knowledge);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        }
        else if(id != null && knowledge != null){
            map.put("id",id);
            map.put("knowledge",knowledge);
            queryWrapper.allEq(map);
        }else if(id != null && chapter != null){
            map.put("id",id);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        } else if (knowledge != null && chapter != null) {
            map.put("knowledge",knowledge);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        }else if(id != null){
            queryWrapper.eq("id",id);
        } else if (knowledge != null) {
            queryWrapper.eq("knowledge",knowledge);
        } else if (chapter != null) {
            queryWrapper.eq("chapter",chapter);
        }
//        List<QuestionNature> questionNatureList = questionNatureMapper.selectList(queryWrapper);
        page(pages,queryWrapper);
        List<QuestionNature> list=pages.getRecords();
        PageVo pageVo=new PageVo(list,pages.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    public ResponseResult questionNatureList(Integer id,String knowledge,String chapter){
        QueryWrapper<QuestionNature> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        if(id != null && knowledge != null && chapter != null){
            map.put("id",id);
            map.put("knowledge",knowledge);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        }
        else if(id != null && knowledge != null){
            map.put("id",id);
            map.put("knowledge",knowledge);
            queryWrapper.allEq(map);
        }else if(id != null && chapter != null){
            map.put("id",id);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        } else if (knowledge != null && chapter != null) {
            map.put("knowledge",knowledge);
            map.put("chapter",chapter);
            queryWrapper.allEq(map);
        }else if(id != null){
            queryWrapper.eq("id",id);
        } else if (knowledge != null) {
            queryWrapper.eq("knowledge",knowledge);
        } else if (chapter != null) {
            queryWrapper.eq("chapter",chapter);
        }
        List<QuestionNature> questionNatureList = questionNatureMapper.selectList(queryWrapper);
        return ResponseResult.okResult().ok(questionNatureList);
    }

    /**
     * 根据题目id返回匹配的题目性质(供本地调用)
     * @param id 题目id
     * @return 返回匹配的题目性质
     */
    @Override
    public QuestionNature getQuestionNatureByIdLocal(Integer id) {
        LambdaQueryWrapper<QuestionNature> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(id),QuestionNature::getId,id);
        QuestionNature questionNature=getOne(lambdaQueryWrapper);
        return questionNature;
    }

    /**
     * 根据题目id数组返回匹配的题目性质数组(供本地调用)
     * @param ids 题目id数组
     * @return  返回匹配的题目性质数组
     */
    @Override
    public List<QuestionNature> getQuestionNatureByIdsLocal(List ids) {
        List<QuestionNature> questionsNature = questionNatureMapper.selectBatchIds(ids);
        return questionsNature;
    }

    /**
     * 添加题目性质
     * @param questionNatureDTO 题目性质实体类
     * @return  添加操作是否成功
     */
    @Override
    public ResponseResult addQuestionNature(QuestionNatureDTO questionNatureDTO) {
        QuestionNature questionNature= BeanCopyUtils.copyBean(questionNatureDTO,QuestionNature.class);
        questionNature.setErrorRate("0");
        questionNatureMapper.insert(questionNature);
        return new ResponseResult(200,"添加题目性质成功",questionNature);
    }

    /**
     * 根据题目id删除匹配的题目性质
     * @param id 要删除题目性质的题目id
     * @return  删除操作是否成功
     */
    @Override
    public ResponseResult deleteQuestionNature(Integer id) {
        if (getById(id)==null){
            //不存在id，提示出错
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,"对不起，删除的题目id不存在！");
        }
        removeById(id);
        return new ResponseResult(200,"删除题目性质成功");
    }

    /**
     * 根据题目id修改匹配的题目性质
     * @param questionNatureDTO 题目性质实体类
     * @return 返回修改操作是否成功
     */
    @Override
    public ResponseResult updateQuestionNature(QuestionNatureDTO questionNatureDTO) {
        if(getById(questionNatureDTO.getId())==null){
            //不存在id，提示出错
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,"对不起，更改信息的题目id不存在！");
        }
        QuestionNature questionNature= BeanCopyUtils.copyBean(questionNatureDTO,QuestionNature.class);
        questionNatureMapper.updateById(questionNature);
        return new ResponseResult(200,"修改题目性质成功",questionNature);
    }

    /**
     * 根据题目id返回匹配的题目性质
     * @param id 题目id
     * @return 返回匹配的题目性质信息
     */
    @Override
    public ResponseResult getQuestionNature(Integer id) {
        if (getById(id)==null){
            //不存在id，提示出错
            return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,"对不起，查询的题目id不存在！");
        }
        QueryWrapper<QuestionNature> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        QuestionNature questionNature = questionNatureMapper.selectOne(queryWrapper);
        String type = questionService.getTypeByIdLocal(questionNature.getId());
        QuestionNatureDTO questionNatureDTO= BeanCopyUtils.copyBean(questionNature,QuestionNatureDTO.class);
        questionNatureDTO.setType(type);
        return ResponseResult.okResult(questionNatureDTO);
    }
}
